/******************************************************************************
 * * mmi_modem_adaptor_sim.h - data structure for mmi-modem adpator, sim sub-module
 *
 * *(C) Copyright 2019 Asr International Ltd.
 * * All Rights Reserved
 * ******************************************************************************/
#ifndef MMI_MODEM_ADAPTOR_SIM_H
#define MMI_MODEM_ADAPTOR_SIM_H

#ifdef __cplusplus
extern "C" {
#endif

#include <string.h>
#include "mmi_modem_adaptor_main.h"
#include "mmi_modem_adaptor_nw.h"

#define MMI_MODEMADP_SIM_READ_PB_NUM    (500) // the maximum number of the contacts read once

/******************************************************************************
 *
 *         Message structure of MMI -> Modem Adaptor
 *
 *******************************************************************************/
// msg stru: MMI_MODEMADP_TRY_PIN_REQ/MMI_MODEMADP_TRY_PIN2_REQ
// MMI_MODEMADP_CHG_PIN_REQ/MMI_MODEMADP_CHG_PIN2_REQ
typedef struct {
    MMI_MODEM_SIM_ID SimId;
    INT8             *Code;
} MMI_Modemadp_Try_Pin_Req_t;

// msg stru: MMI_MODEMADP_TRY_PUK_REQ/MMI_MODEMADP_TRY_PUK2_REQ
typedef struct {
    MMI_MODEM_SIM_ID SimId;
    INT8             *PUK;
    INT8             *NewPIN;
} MMI_Modemadp_Try_Puk_Req_t;

// msg stru: MMI_MODEMADP_CHG_PIN_REQ/MMI_MODEMADP_CHG_PIN2_REQ
typedef struct {
    MMI_MODEM_SIM_ID SimId;
    INT8             *PIN;
    INT8             *OldPIN;
} MMI_Modemadp_Chg_Pin_Req_t;

// msg stru: MMI_MODEMADP_ENABLE_PIN_LOCK
typedef struct {
    MMI_Modem_Pin_Lock_t *PinLockSet;
    INT8                 *PIN;
} MMI_Modemadp_Enable_Pin_Lock_t;

// msg stru: MMI_MODEMADP_QUERY_FACILITY_PIN_LOCK
typedef struct {
    MMI_MODEM_SIM_ID     SimId;
    MMI_Modem_Pin_Lock_t *PinLockSet;
} MMI_Modemadp_Query_Facility_Pin_Lock_t;

// msg stru: MMI_MODEMADP_QUERY_PIN_LOCK
// typedef struct
// {
// MMI_MODEM_SIM_ID   SimId;
// MMI_MODEM_PIN_TYPE PinType;
// BOOL               PinEnable;
// UINT8              RetriesLeft;
// } MMI_Modem_Pin_Lock_t;

// msg stru: MMI_MODEMADP_SIM_PRESENT_CHK_REQ
typedef struct {
    MMI_MODEM_SIM_ID SimId;
} MMI_Modemadp_Sim_Present_Chk_Req_t;

// msg stru: MMI_MODEMADP_GET_PB_STATUS_REQ
typedef struct {
    MMI_MODEM_SIM_ID SimId;
} MMI_Modemadp_Get_Pb_Status_Req_t;

// msg stru: MMI_MODEMADP_WRITE_PB_REQ
typedef struct {
    MMI_MODEM_SIM_ID  SimId;
    MMI_Modem_Contact *Contact;
} MMI_Modemadp_Write_Pb_Req_t;

// msg stru: MMI_MODEMADP_READ_PB_REQ
typedef struct {
    MMI_MODEM_SIM_ID SimId;
    UINT16           Used;
    UINT16           FirstIndex;
} MMI_Modemadp_Read_Pb_Req_t;

// msg stru: MMI_MODEMADP_GET_IMSI_REQ
typedef struct {
    MMI_MODEM_SIM_ID SimId;
} MMI_Modemadp_Get_Imsi_Req_t;

// msg stru: MMI_MODEMADP_GET_ICCID_REQ
typedef struct {
    MMI_MODEM_SIM_ID SimId;
} MMI_Modemadp_Get_Iccid_Req_t;

// msg stru: MMI_MODEMADP_GET_PLMN_REQ
typedef struct {
    MMI_MODEM_SIM_ID SimId;
} MMI_Modemadp_Get_Plmn_Req_t;

// msg stru: MMI_MODEMADP_GET_MSISDN_REQ
typedef struct {
    MMI_MODEM_SIM_ID SimId;
} MMI_Modemadp_Get_Msisdn_Req_t;

// msg stru: MMI_MODEMADP_SEL_CHAR_SET_REQ
typedef struct {
    MMI_MODEM_SIM_ID SimId;
} MMI_Modemadp_Sel_Char_Set_Req_t;

// msg stru: MMI_MODEMADP_SIM_SOFT_RESET_REQ
typedef struct {
    MMI_MODEM_SIM_ID SimId;
} MMI_Modemadp_Sim_Soft_Reset_Req_t;

/******************************************************************************
 *
 *         Message structure of RIL -> Modem Adaptor
 *
 *******************************************************************************/

// msg stru: RIL_MODEMADP_SIM_RSP_CP_PB_INITED
typedef struct {
    RIL_SIM_ID SimId;
} RIL_Modemadp_Rsp_Cp_Pb_Inited_t;

// msg stru: RIL_MODEMADP_SIM_RSP_GET_PB_STATUS
typedef struct {
    RIL_SIM_ID  SimId;
    CPPhoneBook *p_phonebook;
    RIL_Errno   result;
} RIL_Modemadp_Rsp_Get_Pb_Status_t;

// msg stru: RIL_MODEMADP_SIM_RSP_WRITE_PB
typedef struct {
    RIL_SIM_ID SimId;
    INT32      *p_writtenIndex;
    RIL_Errno  result;
} RIL_Modemadp_Rsp_Write_Pb_t;

// msg stru: RIL_MODEMADP_SIM_RSP_READ_PB
typedef struct {
    RIL_SIM_ID  SimId;
    RIL_Contact *p_contact;
    UINT16      count;
    RIL_Errno   result;
} RIL_Modemadp_Rsp_Read_Pb_t;

// msg stru: RIL_MODEMADP_SIM_STATUS_CHANGED
typedef struct {
    RIL_SIM_ID SimId;
} RIL_Modemadp_Rsp_Sim_Status_Changed_t;

// msg stru: RIL_MODEMADP_SIM_RSP_GET_SIM_STATUS
typedef struct {
    RIL_SIM_ID     SimId;
    RIL_CardStatus *p_cardStatus;
    RIL_Errno      result;
} RIL_Modemadp_Rsp_Get_Sim_Status_t;

// msg stru: RIL_MODEMADP_SIM_RSP_QUERY_LOCK_INFO
typedef struct {
    RIL_SIM_ID SimId;
    INT32      *response;
    RIL_Errno  result;
} RIL_Modemadp_Rsp_Query_Lock_Info_t;

// msg stru: RIL_MODEMADP_RSP_FACILITY_LOCK
typedef struct {
    RIL_SIM_ID SimId;
    INT32      *data;
    RIL_Errno  result;
} RIL_Modemadp_Rsp_Set_Facility_Lock_t;

// msg stru: RIL_MODEMADP_RSP_QUERY_FACILITY_LOCK
typedef struct {
    RIL_SIM_ID SimId;
    INT32      *data;
    RIL_Errno  result;
} RIL_Modemadp_Rsp_Query_Facility_Lock_t;

// msg stru: RIL_MODEMADP_SIM_RSP_ENTER_SIM_PIN/RIL_MODEMADP_SIM_RSP_ENTER_SIM_PIN2
// RIL_MODEMADP_SIM_RSP_ENTER_SIM_PUK/RIL_MODEMADP_SIM_RSP_ENTER_SIM_PUK2
// RIL_MODEMADP_SIM_RSP_CHG_SIM_PIN/RIL_MODEMADP_SIM_RSP_CHG_SIM_PIN2
typedef struct {
    RIL_SIM_ID SimId;
    INT32      *p_retryLeft;
    RIL_Errno  result;
} RIL_Modemadp_Rsp_Enter_Sim_Pin_t;

// msg stru: RIL_MODEMADP_SIM_RSP_GET_IMSI
typedef struct {
    RIL_SIM_ID SimId;
    INT8       *p_imsi;
    RIL_Errno  result;
} RIL_Modemadp_Rsp_Get_Imsi_t;

// msg stru: RIL_MODEMADP_SIM_RSP_GET_ICCID
typedef struct {
    RIL_SIM_ID   SimId;
    RIL_Errno    result;
    char         *p_iccid;
} RIL_Modemadp_Rsp_Get_Iccid_t;

// msg stru: RIL_MODEMADP_SIM_RSP_GET_PLMN
typedef struct {
    RIL_SIM_ID   SimId;
    RIL_Errno    result;
    RIL_SIM_Plmn_Info *p_plmn;
} RIL_Modemadp_Rsp_Get_Plmn_t;

// msg stru: RIL_MODEMADP_SIM_RSP_GET_MSISDN
typedef struct {
    RIL_SIM_ID   SimId;
    RIL_Errno    result;
    char         *p_msisdn;
} RIL_Modemadp_Rsp_Get_Msisdn_t;

typedef enum {
    MODEMADP_SIM_PB_WRITING = 0,
    MODEMADP_SIM_PB_READING,
    MODEMADP_SIM_PB_STATUS_GETTING,
    MODEMADP_SIM_PIN_TRYING,
    MODEMADP_SIM_PIN2_TRYING,
    MODEMADP_SIM_PUK_TRYING,
    MODEMADP_SIM_PUK2_TRYING,
    MODEMADP_SIM_PIN_CHANGING,
    MODEMADP_SIM_PIN2_CHANGING,
    MODEMADP_SIM_PIN_LOCK_QUERYING,
    MODEMADP_SIM_SIM_PRESENT_CHECKING,
    MODEMADP_SIM_IMSI_GETTING,
    MODEMADP_SIM_GET_ICCID,
    MODEMADP_SIM_GET_PLMN,
    MODEMADP_SIM_GET_MSISDN,
    MODEMADP_SIM_IDLE,
} MODEMADP_SIM_OPERATION_STATUS;

typedef struct {
    char regMnc[4];   ///register Mnc 3 character
    char regMcc[4];
    char homeMnc[4];   ///home Mnc 3 characters
    char homeMcc[4];
    int  signalstrength; /// range is 0-31
    int  lac;
    int  cid;
} MODEMADP_SIM_REG_AND_HOME_PLMN_INFO;

typedef struct {
    BOOL                             SimPresent[2];               // true means SIMx present, init to false
    SEMA_ID                          *PinCheckSema;               // Semaphore to lock UI thread during pin checking, init to 0
    UINT8                            PinCheckRetryLeft;
    UINT8                            QueryFacPinLockResult;
    MMI_MODEM_PIN_TYPE               QueryPinLockType;
    BOOL                             QueryPinLockResult;

    MODEMADP_SIM_OPERATION_STATUS    SimOperationStatus[2];
    MMI_Modem_Contact                *WritePbContact[2];
    MMI_Modem_Contact                *ReadPbContact[2];
    UINT16                           ContactsUsed[2];

    INT8                             *Imsi[2];
    MODEMADP_SIM_REG_AND_HOME_PLMN_INFO RegHomePlmn[2];
    char                             *Iccid[2];
    char                             *Msisdn[2];
} Modem_SIM_Mng_t;

extern Modem_SIM_Mng_t g_ModemSimMng;

#ifdef __cplusplus
} /* extern "C" */
#endif

#endif /*SETTING_CALL_H*/
